﻿using Sustainalytics.DataLayer.ConnectionStrings;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.ProductInvolvement.BL;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.ProductInvolvement.Service.Models;
using System.Web.Http.Cors;

namespace Sustainalytics.ProductInvolvement.Service
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/piindicator/{symbol}/categories")]
    [LogControllerFilter(true, true, false)]
    public class PIIndicatorCategoriesController : ApiController, IPIIndicatorCategoriesController
    {
        IProductInvolvementBussinesLogic _blLayer;

        public PIIndicatorCategoriesController(IProductInvolvementBussinesLogic blLayer)
        {
            _blLayer = blLayer;
        }

        [Route("")]
        public HttpResponseMessage Get([FromUri] string symbol, bool includeAllFields = false)
        {
            try
            {
                var categories = _blLayer.GetIndicatorCategories(symbol).ToList();

                List<ProductInvolvementFrameworkCategoryDto> finalCategories = new List<ProductInvolvementFrameworkCategoryDto>();
                foreach (var cat in categories)
                {
                    Guid templateCriteriaId = _blLayer.GetTemplateCriteriaIdByPISymbol(cat.Symbol);
                    finalCategories.Add(ProductInvolvementFrameworkCategoryDto.CreateComplete(cat, templateCriteriaId));
                }
                return Request.CreateResponse(HttpStatusCode.OK, finalCategories);
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new { ErrorCode = 1010 });
            }
        }
    }
}
